package com.qinggeng.project.system.service.impl;

import java.util.List;

import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.project.api.dto.MessageListDto;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.mapper.UserAliasMapper;
import com.qinggeng.project.system.mapper.UserMessageIconMapper;
import com.qinggeng.project.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qinggeng.project.system.mapper.UserMessageMapper;

import javax.annotation.Resource;

/**
 * 消息列Service业务层处理
 *
 * @author qinggeng
 * @date 2020-06-30
 */
@Slf4j
@Service
public class UserMessageServiceImpl implements IUserMessageService
{



    @Resource
    private UserMessageMapper userMessageMapper;

    @Autowired
    private IUserAliasService userAliasService;
    @Autowired
    private IOrdersPointService ordersPointService;
    @Autowired
    private IDistributionPointService pointService;
    @Resource
    private UserAliasMapper userAliasMapper;
    @Autowired
    private UserMessageIconService messageIconService;
    @Resource
    private UserMessageIconMapper messageIconMapper;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrdersGoodsService ordersGoodsService;


    /**
     * 查询消息列
     *
     * @param messageId 消息列ID
     * @return 消息列
     */
    @Override
    public UserMessage selectUserMessageById(Long messageId)
    {
        return userMessageMapper.selectUserMessageById(messageId);
    }

    /**
     * 查询消息列列表
     *
     * @param userMessage 消息列
     * @return 消息列
     */
    @Override
    public List<UserMessage> selectUserMessageList(UserMessage userMessage)
    {
        return userMessageMapper.selectUserMessageList(userMessage);
    }

    /**
     * 新增消息列
     *
     * @param userMessage 消息列
     * @return 结果
     */
    @Override
    public int insertUserMessage(UserMessage userMessage)
    {
        userMessage.setCreateTime(DateUtils.getNowDate());
        return userMessageMapper.insertUserMessage(userMessage);
    }
    /**
     * 修改消息列
     *
     * @param userMessage 消息列
     * @return 结果
     */
    @Override
    public int updateUserMessage(UserMessage userMessage)
    {
        return userMessageMapper.updateUserMessage(userMessage);
    }

    /**
     * 批量删除消息列
     *
     * @param messageIds 需要删除的消息列ID
     * @return 结果
     */
    @Override
    public int deleteUserMessageByIds(Long[] messageIds)
    {
        return userMessageMapper.deleteUserMessageByIds(messageIds);
    }

    /**
     * 删除消息列信息
     *
     * @param messageId 消息列ID
     * @return 结果
     */
    @Override
    public int deleteUserMessageById(Long messageId)
    {
        return userMessageMapper.deleteUserMessageById(messageId);
    }

    @Override
    public List<UserMessage> selectUserMessageMemberList(UserMessage where) {
        return userMessageMapper.selectUserMessageMemberList(where);
    }



    @Override
    public List<MessageListDto> listMessageListDto(UserMessage where) {
        userMessageMapper.updateUserMessageRead(where);
        return userMessageMapper.listMessageListDto(where);
    }

    @Override
    public int selectUserMessageCount(UserMessage messageWhere) {
        return userMessageMapper.selectUserMessageCount(messageWhere);
    }

    @Override
    public UserMessage selectFirstUserMessageList(Long userId) {
        return userMessageMapper.selectFirstUserMessage(userId);
    }



    @Override
    public boolean delMessage(String messageId) {
        Long[] ids= (Long[]) ConvertUtils.convert(messageId.split(","),Long.class);
        int rows=0;
        for (Long id:ids) {
            UserMessage userMessage = new UserMessage();
            userMessage.setMessageId(id);
            userMessage.setDelFlag("0");
            if(updateUserMessage(userMessage)>0){
                rows++;
            };
        }
        return rows >0;
    }

    /**
     * 通过订单Id和状态查询消息
     * @param ordersId
     * @return
     */
    @Override
    public int selectUserMessageByIdAndStatus(Long ordersId) {
        return userMessageMapper.selectUserMessageByIdAndStatus(ordersId);
    }

}
